package com.diploma.project.aiqiyi.common.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ThreadPoolClient {

	@Autowired
	private ThreadPoolConfig threadPoolConfig;

	@Bean
	public ThreadPoolTaskExecutor getAsyncExecutor() {
		ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
		taskExecutor.setCorePoolSize(threadPoolConfig.getCorePoolSize());
		taskExecutor.setMaxPoolSize(threadPoolConfig.getMaxPoolSize());
		taskExecutor.setQueueCapacity(threadPoolConfig.getQueueCapacity());
		taskExecutor.setKeepAliveSeconds(threadPoolConfig.getKeepAliveSeconds());
		taskExecutor.setThreadFactory(tr -> new Thread(tr, "async-thread-aiqiyi_" + tr.hashCode()));
		taskExecutor.initialize();
		return taskExecutor;
	}

}
